package com.example.uniquekey;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class DbUtils {

    /**
     * 判断是否是主键重复异常
     *
     * @param e 参数异常
     * @return true 是 / false 否
     */
    public static boolean isDupUniqueKeyException(Throwable e) {
        SQLException sqlException = null;
        while (true) {
            if (e == null) {
                return false;
            }
            if (e instanceof SQLException) {
                sqlException = (SQLException) e;
                break;
            }
            e = e.getCause();
        }
        int errorCode = sqlException.getErrorCode();
        String sqlState = sqlException.getSQLState();
        // 1062 为主键冲突 Duplicate entry ‘%s’ for key %d。 2300为主键重复约束
        return errorCode == 1062 && "23000".equals(sqlState);
    }
}